﻿using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using QuanLyHopDong.Biz.Model;

namespace QuanLyHopDong.Biz
{
    public class QuyetToanBiz
    {
        public HopDongView GetHopDongData(int hopDongId)
        {
            using (var db = new HopDongEntities())
            {
                return db.HopDongViews.FirstOrDefault(x => x.Id == hopDongId);
            }
        }

        public List<ThanhToan> GetThanhToans(int hopDongId)
        {
            using (var db = new HopDongEntities())
            {
                return db.ThanhToans.Where(x => x.HopDongId == hopDongId).ToList();
            }
        }

        public void AddThanhToan(ThanhToan thanhToan)
        {
            if (thanhToan == null) return;
            if (thanhToan.HopDongId <= 0) return;
            if (string.IsNullOrWhiteSpace(thanhToan.Ten)) return;

            using (var db = new HopDongEntities())
            {
                db.ThanhToans.Add(thanhToan);
                db.SaveChanges();
            }
        }

        public void UpdateThanhToan(ThanhToan thanhToan)
        {
            if (thanhToan == null) return;
            if (thanhToan.Id <= 0) return;
            if (thanhToan.HopDongId <= 0) return;
            if (string.IsNullOrWhiteSpace(thanhToan.Ten)) return;

            using (var db = new HopDongEntities())
            {
                db.ThanhToans.Attach(thanhToan);
                db.Entry(thanhToan).State = EntityState.Modified;
                db.SaveChanges();
            }
        }

        public void DeleteChungTu(string filePath, int thanhToanId)
        {
            if (File.Exists(filePath))
                File.Delete(filePath);

            using (var db = new HopDongEntities())
            {
                db.DeleteChungTu(thanhToanId);
            }
        }

        public void DeleteFileChungTu(string folderPath, int thanhToanId)
        {
            using (var db = new HopDongEntities())
            {
                var thanhToan = db.ThanhToans.FirstOrDefault(x => x.Id == thanhToanId);
                if (thanhToan == null) return;
                if (string.IsNullOrEmpty(thanhToan.ChungTu)) return;
                var filePath = string.Format("{0}\\{1}\\{2}", folderPath, thanhToan.HopDongId, thanhToan.ChungTu);

                if (File.Exists(filePath))
                    File.Delete(filePath);

                db.DeleteChungTu(thanhToanId);
            }
        }

        public void DeleteThanhToan(string folderPath, int id)
        {
            using (var db = new HopDongEntities())
            {
                var thanhToan = db.ThanhToans.FirstOrDefault(x => x.Id == id);
                if (thanhToan == null) return;
                if (!string.IsNullOrEmpty(thanhToan.ChungTu))
                {
                    var filePath = string.Format("{0}\\{1}\\{2}", folderPath, thanhToan.HopDongId, thanhToan.ChungTu);

                    if (File.Exists(filePath))
                        File.Delete(filePath);
                }

                db.ThanhToans.Remove(thanhToan);
                db.SaveChanges();
            }
        }

        public ThanhToan GetThanhToan(int id)
        {
            using (var db = new HopDongEntities())
            {
                return db.ThanhToans.FirstOrDefault(x => x.Id == id);
            }
        }

        public QuyetToan GetQuyetToan(int hopDongId)
        {
            using (var db = new HopDongEntities())
            {
                return db.QuyetToans.FirstOrDefault(x => x.HopDongId == hopDongId);
            }
        }

        public void CreateQuyetToan(QuyetToan quyetToan)
        {
            using (var db = new HopDongEntities())
            {
                // check exist
                var count = db.QuyetToans.Count(x => x.HopDongId.Equals(quyetToan.HopDongId));
                if (count > 0)
                {
                    // existing, update
                    db.QuyetToans.Attach(quyetToan);
                    db.Entry(quyetToan).State = EntityState.Modified;
                }
                else
                {
                    // create new
                    db.QuyetToans.Add(quyetToan);
                }

                db.SaveChanges();
            }
        }

        public List<QuyetToanNoiBo> GetQuyetToanNoiBos(int hopDongId)
        {
            using (var db = new HopDongEntities())
            {
                return db.QuyetToanNoiBoes.Where(x => x.HopDongId == hopDongId).ToList();
            }
        }

        public void AddQuyetToanNoiBo(QuyetToanNoiBo quyetToanNoiBo)
        {
            if (quyetToanNoiBo == null) return;
            if (quyetToanNoiBo.HopDongId <= 0) return;
            if (string.IsNullOrWhiteSpace(quyetToanNoiBo.Ten)) return;

            using (var db = new HopDongEntities())
            {
                db.QuyetToanNoiBoes.Add(quyetToanNoiBo);
                db.SaveChanges();
            }
        }

        public void DeleteQuyetToanNoiBo(string folderPath, int id)
        {
            using (var db = new HopDongEntities())
            {
                var quyetToanNoiBo = db.QuyetToanNoiBoes.FirstOrDefault(x => x.Id == id);
                if (quyetToanNoiBo == null) return;
                if (!string.IsNullOrEmpty(quyetToanNoiBo.ChungTu))
                {
                    var filePath = string.Format("{0}\\{1}\\{2}", folderPath, quyetToanNoiBo.HopDongId,
                        quyetToanNoiBo.ChungTu);

                    if (File.Exists(filePath))
                        File.Delete(filePath);
                }

                db.QuyetToanNoiBoes.Remove(quyetToanNoiBo);
                db.SaveChanges();
            }
        }

        public void DeleteFileQuyetToanNoiBoChungTu(string folderPath, int quyetToanNoiBoId)
        {
            using (var db = new HopDongEntities())
            {
                var quyetToanNoiBo = db.QuyetToanNoiBoes.FirstOrDefault(x => x.Id == quyetToanNoiBoId);

                if (quyetToanNoiBo == null) return;
                if (string.IsNullOrEmpty(quyetToanNoiBo.ChungTu)) return;

                var filePath = string.Format("{0}\\{1}\\{2}", folderPath, quyetToanNoiBo.HopDongId, quyetToanNoiBo.ChungTu);
                if (File.Exists(filePath))
                    File.Delete(filePath);

                db.DeleteQuyetToanNoiBoChungTu(quyetToanNoiBoId);
            }
        }

        public QuyetToanNoiBo GetQuyetToanNoiBo(int id)
        {
            using (var db = new HopDongEntities())
            {
                return db.QuyetToanNoiBoes.FirstOrDefault(x => x.Id == id);
            }
        }

        public void DeleteQuyetToanNoiBoChungTu(string filePath, int id)
        {
            if (File.Exists(filePath))
                File.Delete(filePath);

            using (var db = new HopDongEntities())
            {
                db.DeleteQuyetToanNoiBoChungTu(id);
            }
        }

        public void UpdateQuyetToanNoiBo(QuyetToanNoiBo quyetToanNoiBo)
        {
            if (quyetToanNoiBo == null) return;
            if (quyetToanNoiBo.Id <= 0) return;
            if (quyetToanNoiBo.HopDongId <= 0) return;
            if (string.IsNullOrWhiteSpace(quyetToanNoiBo.Ten)) return;

            using (var db = new HopDongEntities())
            {
                db.QuyetToanNoiBoes.Attach(quyetToanNoiBo);
                db.Entry(quyetToanNoiBo).State = EntityState.Modified;
                db.SaveChanges();
            }
        }

        public object GetTotalThanhToan(int hopDongId)
        {
            using (var db = new HopDongEntities())
            {
                var totalDaThanhToan = db.ThanhToans.Where(x => x.HopDongId == hopDongId && x.DaThanhToan).Sum(x => x.SoTien);
                var totalChuaThanhToan = db.ThanhToans.Where(x => x.HopDongId == hopDongId && !x.DaThanhToan).Sum(x => x.SoTien);
                return new { TotalDaThanhToan = totalDaThanhToan, TotalChuaThanhToan = totalChuaThanhToan };
            }
        }

        public object GetTotalQuyetToan(int hopDongId)
        {
            using (var db = new HopDongEntities())
            {
                var totalVatTuQuyetToan =
                    db.QuyetToanNoiBoes.Where(x => x.HopDongId == hopDongId && x.LoaiQuyetToanId == (short)LoaiQuyetToan.VatTu)
                        .Sum(x => x.SoTien);
                var totalNhanCongQuyetToan =
                    db.QuyetToanNoiBoes.Where(x => x.HopDongId == hopDongId && x.LoaiQuyetToanId == (short)LoaiQuyetToan.NhanCong)
                        .Sum(x => x.SoTien);

                return new { TotalVatTuQuyetToan = totalVatTuQuyetToan, TotalNhanCongQuyetToan = totalNhanCongQuyetToan };
            }
        }
    }
}
